<template>
  <basic-container>
    <div class="avue-crud">

      <el-tabs v-model="clientId"
               type="card"
               @tab-click="seachList">
        <el-tab-pane :label="item.label"
                     :name="item.clientId"
                     v-for="(item,index) in menuClients"
                     :key="index" />
      </el-tabs>

      <div class="avue-crud__menu">
        <div class="avue-crud__left">
          <el-button v-if="permissions.menu_add"
                     icon="el-icon-plus"
                     type="primary"
                     @click="addOrUpdateHandle(false)">
            添加
          </el-button>
        </div>
        <div class="avue-crud__right">
          <el-tooltip class="item"
                      effect="dark"
                      content="刷新"
                      placement="top">
            <el-button icon="el-icon-refresh"
                       class="is-circle"
                       @click="getList()">
            </el-button>
          </el-tooltip>
        </div>
      </div>

      <el-table border
                v-loading="loading"
                :data="menuList"
                row-key="id"
                :tree-props="{children: 'children', hasChildren: 'hasChildrens'}">
        <el-table-column prop="name"
                         label="菜单名称"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="icon"
                         label="图标"
                         align="center"
                         width="120">
          <template slot-scope="scope">
            <i :class="scope.row.icon" />
          </template>
        </el-table-column>
        <el-table-column prop="sort"
                         label="排序"
                         width="80"></el-table-column>
        <el-table-column prop="path"
                         label="组件路径"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="type"
                         label="类型"
                         width="150"
                         align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type === '0'">左菜单</el-tag>
            <el-tag type="success"
                    v-if="scope.row.type === '2'">顶菜单</el-tag>
            <el-tag type="warning"
                    v-if="scope.row.type === '1'">按钮</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="keepAlive"
                         label="缓冲"
                         width="100"
                         align="center">
          <template slot-scope="scope">
            <el-tag type="info"
                    v-if="scope.row.keepAlive === '0'">关闭</el-tag>
            <el-tag type="success"
                    v-if="scope.row.keepAlive === '1'">开启</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="permission"
                         label="权限标识"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作"
                         align="center"
                         class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="small"
                       type="text"
                       icon="el-icon-plus"
                       @click="addOrUpdateHandle(false,scope.row.id)"
                       v-if="permissions.menu_add">新增
            </el-button>
            <el-button size="small"
                       type="text"
                       icon="el-icon-edit"
                       @click="addOrUpdateHandle(true,scope.row.id)"
                       v-if="permissions.menu_edit">修改
            </el-button>
            <el-button size="small"
                       type="text"
                       icon="el-icon-delete"
                       @click="handleDelete(scope.row)"
                       v-if="permissions.menu_del">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <table-form v-if="addOrUpdateVisible"
                  ref="addOrUpdate"
                  :clientId="clientId"
                  @refreshDataList="getList" />
    </div>
  </basic-container>
</template>

<script>
import { delObj, fetchMenuTree } from '@/api/admin/menu'
import TableForm from './menu-form'
import { mapGetters } from 'vuex'

export default {
  name: "Menu",
  components: { TableForm },
  data () {
    return {
      addOrUpdateVisible: false,
      // 遮罩层
      loading: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      // 菜单所属客户端列表
      menuClients: [{ clientId: 'pcSide', label: 'PC端' }, { clientId: 'mobileSide', label: '移动端' }],
      // 默认选中客户端
      clientId: 'pcSide'
    };
  },
  created () {
    this.getList();
  },
  computed: {
    ...mapGetters(['permissions']),
  },
  methods: {
    addOrUpdateHandle (isEdit, id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(isEdit, id)
      })
    },
    seachList () {
      this.getList()
    },
    getList () {
      this.loading = true;
      fetchMenuTree(this.clientId, false).then(response => {
        this.menuList = response.data.data || []
        this.loading = false;
      });
    },
    handleDelete (row) {
      this.$confirm('是否确认删除名称为"' + row.name + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delObj(row.id);
      }).then(() => {
        this.getList();
        this.$message.success('删除成功')
      })
    }
  }
};
</script>